package com.bsh.controller.partner;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bsh.dto.PartnerEditDto;
import com.bsh.mybatis.dataView.PartnerExt;
import com.bsh.mybatis.model.Partner;
import com.bsh.mybatis.model.PartnerLevel;
import com.bsh.mybatis.model.Region;
import com.bsh.service.PartnerLevelService;
import com.bsh.service.PartnerService;
import com.bsh.service.RegionService;
import com.bsh.utils.IpUtil;
import com.bsh.utils.PageJosnMessage;
import com.bsh.utils.PageTools;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

@Controller
public class PartnerAskController {
	private static Logger log = LoggerFactory.getLogger(PartnerAskController.class);

	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	private PartnerService partService;
	@Autowired
	RegionService  regServ;
	@Autowired
	PartnerLevelService levelServ;
	
	/**
	 * 合作伙伴申请列表
	 */
	@RequestMapping(value = "/partner/ask/list")
	public ModelAndView partnerAskList() {
		String userName = request.getParameter("userName") == null ? null : request.getParameter("userName").toString();
		String mobile = request.getParameter("mobile") == null ? null : request.getParameter("mobile").toString();

		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/partner/partnerAskList");

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = partService.getAskPage(pager, userName, mobile);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		mv.addObject("userName", userName);
		mv.addObject("mobile", mobile);
		return mv;
	}
	
	
	/**
	 * 审核通过
	 */
	@ResponseBody
	@RequestMapping(value = "/partner/audit/pass/{mid}")
	public PageJosnMessage pass(@PathVariable(value="mid") Integer mid) {
		String ip=IpUtil.getIpAddr(request);
        Integer res =  partService.audit(mid, true,null,ip);
        PageJosnMessage msg=new PageJosnMessage();
        res=(res==null?1:res);
        msg.putData("status",res>0?1:res);
		return msg;
	}
	
	/**
	 * 审核未通过
	 */
	@ResponseBody
	@RequestMapping(value = "/partner/audit/refuse")
	public ModelAndView refuse(@RequestParam(value="mid") Integer mid,@RequestParam(value="msg") String auditMsg) {
		String ip=IpUtil.getIpAddr(request);
        Integer res =  partService.audit(mid, false,auditMsg,ip);
        ModelAndView mv=refuseMsg(mid);
        mv.addObject("mid", mid);
        mv.addObject("msg", auditMsg);
        
        String msg="";
        if(res==1)
        	msg = PageTools.alertCloseFraRefresh("审核合作伙伴申请","审核成功！",1000);
        else
        	msg = PageTools.alert("审核合作伙伴申请","审核失败，请稍后重试！",1000);
		mv.addObject("actionMsg",msg);
		return mv;
	}

	/**
	 * 添加审核消息
	 */
	@ResponseBody
	@RequestMapping(value = "/partner/audit/msg/{mid}")
	public ModelAndView refuseMsg(@PathVariable(value="mid") Integer mid) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/partner/auditMsg");
		mv.addObject("mid",mid);
		return mv;
	}
	
	

	/**
	 * 添加合作伙伴信息--初使化
	 */
	@RequestMapping(value = "/partner/add")
	public ModelAndView add() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/partner/partnerEdit");
		Partner partner = new Partner();		
		mv.addObject("part", partner);
		if(partner !=null){
			List<Region> prov = regServ.getRegionList(1);			
			List<Region> city = regServ.getRegionList(null);
			List<Region> area = regServ.getRegionList(null);
			mv.addObject("provList",prov);
			mv.addObject("cityList",city);
			mv.addObject("areaList",area);
		}
		return mv;
	}
	

	/**
	 * 修改合作伙伴信息--初使化
	 */
	@RequestMapping(value = "/partner/edit/{id}")
	public ModelAndView edit(@PathVariable(value = "id") Integer mid) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/partner/partnerEdit");
		PartnerExt partner = partService.getPartnerInfo(mid);		
		mv.addObject("part", partner);
		if(partner !=null){
			Integer country=partner.getCountry();
			List<Region> prov = regServ.getRegionList(country==null?1:country);			
			List<Region> city = regServ.getRegionList(partner.getProvince());
			List<Region> area = regServ.getRegionList(partner.getCity());
			mv.addObject("provList",prov);
			mv.addObject("cityList",city);
			mv.addObject("areaList",area);
		}
		List<PartnerLevel> levelList= levelServ.getPartnerLevelList();
		mv.addObject("levelList", levelList);
		return mv;
	}

	/**
	 * 修改合作伙伴信息--保存
	 */
	@RequestMapping(value = "/partner/edit/save")
	public ModelAndView editSave(@ModelAttribute PartnerEditDto dto) {
		BeanCopier copy = BeanCopier.create(PartnerEditDto.class, Partner.class, false);
		Partner partner = new Partner();
		copy.copy(dto, partner, null);
		partner.setCountry(1);
        Integer res = partService.editPartner(partner);
        
        ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/user/userEdit");
		mv.addObject("part", dto);
		
		String msg=PageTools.alertCloseFraRefresh("修改合作伙伴信息",res>0?"保存成功！":"保存失败",1000);
		mv.addObject("actionMsg",msg);
		return mv;
	}
}
